package cn.itcast.job;

import cn.itcast.domain.system.User;
import cn.itcast.service.company.CompanyService;
import cn.itcast.service.system.UserService;
import cn.itcast.utils.MailUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component("birthdayJob")
public class BirthdayJob {
    @Autowired
    private AmqpTemplate amqpTemplate;
    @Reference
    private UserService userService;
    public void SendBirthdayEmial()throws Exception{
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        String currentDate=format.format(new Date());
        List<User>userList= userService.findbycurrentDate(currentDate);
        if(userList!=null&&userList.size()>0) {
            for (User user : userList) {
                Map<String , String> map = new HashMap<>();
                map.put("to" , user.getEmail());
                map.put("subject" , "happy..birthday");
                map.put("content" , "亲爱的："+user.getUserName()+",生日快乐！");
                amqpTemplate.convertAndSend("mail.send" , map);
            }
        }
    }
}
